'use strict';

module.exports = app => {
    return class PostService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }

        /**
         * 获取社区最新帖子和提问帖子
         * @param params
         */
        async getNewPost(params,typeName) {
            let url = this.app.urls('getnewpost');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    forumId:params.id,
                    typeName:typeName,
                    orderBy:-1
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 获取社区热门帖子
         * @param params
         */
        async getHotPost(params) {
            let url = this.app.urls('gethotpost');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    forumId:params.id
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        //社区发帖
        async setPost(params){
            let url = this.app.urls('posting');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url,{
                method: 'post',
                dataType:'json',
                data:{
                    forumId:params.forumId,
                    content:params.content,
                    typeName:params.typeName,
                    title:params.title,
                    intro:params.intro,

                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }



    }

};
